const pool = require('../../config/database')


class models{
    async findAllCompanies(){
      const [rows] = await pool.query(
          'SELECT * FROM companies'
      );
      return rows;
    }

    async finCompaniesById(id){
        const [rows] = await pool.query(
            'SELECT * FROM companies WHERE company_id=?',[id]
        );
        return rows;
    }

    async creatCompany(companyData){
        const {name,company_id,industry_field,financing_stage,min_number,max_number,website,description} = companyData
        const [result] = await pool.query(
            'INSERT INTO companies (name,company_id,industry_field,financing_stage,min_number,max_number,website,description) VALUES (?,?,?,?,?,?,?,?)',
            [name,company_id,industry_field,financing_stage,min_number,max_number,website,description]
        )
        return {id:result.insertId,...companyData}
    }

    async updateCompany(company_id, data) {
      const {
        name,
        logo_url,
        financing_stage,
        company_size,
        industry_field,
        description,
        website
      } = data;

      const sql = `
        UPDATE companies
        SET name = ?, logo_url = ?, financing_stage = ?,
            company_size =?, industry_field = ?,
            description = ?, website = ?
        WHERE company_id = ?
      `;

      const values = [
        name, logo_url, financing_stage,
        company_size, industry_field,
        description, website, company_id
      ];

      // console.log('sql:',sql);
      // console.log('values:',values);
      const [result] = await pool.execute(sql, values);
      return result.affectedRows > 0;
    }

    async updateCompanyLogo(data){
      const { logo_url,company_id } = data;
      const [result] = await pool.execute(
        'UPDATE companies SET logo_url=? WHERE company_id = ?',[logo_url,company_id]
      );
      return result.affectedRows > 0;
    }
    
}


module.exports = new models();